{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  龚剑\n",
    "+ 学号  0163275"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "她是悠悠一抹斜阳\n",
      "多想多想有谁懂得欣赏\n",
      "他有蓝蓝一片云窗\n",
      "只等只等有人与之共享\n",
      "她是绵绵一段乐章\n",
      "多想有谁懂得吟唱\n",
      "他有满满一目柔光\n",
      "只等只等有人为之绽放\n",
      "来啊快活啊\n",
      "反正有大把时光\n",
      "来啊爱情啊反正有大把愚妄\n",
      "来啊流浪啊反正有大把方向\n",
      "来啊造作啊反正有大把风光\n",
      "\n",
      "啊痒\n",
      "大大方方爱上爱的表象\n",
      "迂迂回回迷上梦的孟浪\n",
      "越慌越想越慌越痒越搔越痒\n",
      "\n",
      "她是悠悠一抹斜阳\n",
      "多想多想有谁懂得欣赏\n",
      "他有蓝蓝一片云窗\n",
      "只等只等有人与之共享\n",
      "她是绵绵一段乐章\n",
      "多想有谁懂得吟唱\n",
      "他有满满一目柔光\n",
      "只等只等有人为之绽放\n",
      "来啊快活啊\n",
      "反正有大把时光\n",
      "来啊爱情啊反正有大把愚妄\n",
      "来啊流浪啊反正有大把方向\n",
      "来啊造作啊反正有大把风光\n",
      "\n"
     ]
    }
   ],
   "source": [
    "def re():\n",
    "    print(\"她是悠悠一抹斜阳\\n多想多想有谁懂得欣赏\\n他有蓝蓝一片云窗\\n只等只等有人与之共享\\n她是绵绵一段乐章\\n多想有谁懂得吟唱\\n他有满满一目柔光\\n只等只等有人为之绽放\\n来啊快活啊\\n反正有大把时光\\n来啊爱情啊反正有大把愚妄\\n来啊流浪啊反正有大把方向\\n来啊造作啊反正有大把风光\\n\")\n",
    "re()\n",
    "print(\"啊痒\\n大大方方爱上爱的表象\\n迂迂回回迷上梦的孟浪\\n越慌越想越慌越痒越搔越痒\\n\")\n",
    "re()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值= 6.5\n",
      "样本方差= 11.916666666666666\n",
      "均值= 12\n",
      "均值= 20.214285714285715\n",
      "样本方差= 198.8826530612245\n",
      "均值= 14\n"
     ]
    }
   ],
   "source": [
    "def cal(*x,**t):\n",
    "    y=0\n",
    "    n=len(x)\n",
    "    mean=sum(x)/n\n",
    "    for value in x:\n",
    "        y=pow(value-mean,2)+y\n",
    "    print(\"均值=\",mean)\n",
    "    print(\"样本方差=\",y/n)\n",
    "    print(\"均值=\",n)\n",
    "       \n",
    "    \n",
    "cal(1,2,3,4,5,6,7,8,9,10,11,12)\n",
    "cal(13,14,15,1,6,17,18,19,20,21,22,25,65,27)\n",
    "\n",
    "        \n",
    "        \n",
    "        \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值= 6.5\n",
      "样本方差= 13.0\n",
      "样本量= 12\n",
      "标准差= 3.452052529534663\n",
      "均值= 20.214285714285715\n",
      "样本方差= 214.18131868131866\n",
      "样本量= 14\n",
      "方差= 198.8826530612245\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "def cal(*x,stvar=None,var=None):\n",
    "    y=0\n",
    "    n=len(x)\n",
    "    mean=sum(x)/n\n",
    "    for value in x:\n",
    "        y=pow(value-mean,2)+y\n",
    "    print(\"均值=\",mean)\n",
    "    print(\"样本方差=\",y/(n-1))\n",
    "    print(\"样本量=\",n)\n",
    "    if stvar:\n",
    "        print(\"标准差=\",numpy.sqrt(y/n))\n",
    "    if var:\n",
    "        print(\"方差=\",y/n)\n",
    "       \n",
    "    \n",
    "cal(1,2,3,4,5,6,7,8,9,10,11,12,stvar=1)\n",
    "cal(13,14,15,1,6,17,18,19,20,21,22,25,65,27,var=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "均值= 6.5\n",
      "样本方差= 13.0\n",
      "样本量= 12\n",
      "标准差= 3.452052529534663\n",
      "最大值= 12\n",
      "均值= 20.214285714285715\n",
      "样本方差= 214.18131868131866\n",
      "样本量= 14\n",
      "方差= 198.8826530612245\n",
      "标准误= 3.7690720064874674\n",
      "最小值= 1\n"
     ]
    }
   ],
   "source": [
    "import numpy\n",
    "def cal(*x,stvar=None,var=None,sterror=None,ma=None,mi=None):\n",
    "    y=0\n",
    "    n=len(x)\n",
    "    mean=sum(x)/n\n",
    "    for value in x:\n",
    "        y=pow(value-mean,2)+y\n",
    "    std=numpy.std(x,ddof=0)\n",
    "    print(\"均值=\",mean)\n",
    "    print(\"样本方差=\",y/(n-1))\n",
    "    print(\"样本量=\",n)\n",
    "    if stvar:\n",
    "        print(\"标准差=\",numpy.sqrt(y/n))\n",
    "    if var:\n",
    "        print(\"方差=\",y/n)\n",
    "    if sterror:\n",
    "        print(\"标准误=\",std/numpy.sqrt(n))\n",
    "    if ma:\n",
    "        print(\"最大值=\",max(x))\n",
    "    if mi:\n",
    "        print(\"最小值=\",min(x))\n",
    "    \n",
    "    \n",
    "cal(1,2,3,4,5,6,7,8,9,10,11,12,stvar=1,ma=1)\n",
    "cal(13,14,15,1,6,17,18,19,20,21,22,25,65,27,var=1,sterror=1,mi=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
